;(function ($, window) {
    $.CompareBox = function (options) {
        var _CompareBox = {
            options: {
                targetDiv:"",
                containerClass:"ui-uniquebox",
                maxSize:4,
                uniqueBoxContainerId : 'uniqueBox' + options.targetDiv,
                configSubmitBtn : true,
                submitBtnTip:"提交",
                setValueAbleWhenFull:true,
                tipsWhenFull:"对比栏已满，您可以删除不需要的栏内商品再继续添加哦！",
                onAdd:function (key,box) {},
                onRemove:function (key,box) {},
                onSubmit:function (valCsvParam) {},
                keepPersistedValueHtmlTagConfig : {
                    getKeepPersistedValueHtmlTagSelector:function (key) {},
                    addText:"添加比较",
                    removeText:"删除比较",
                    logoPathAttrName:"logoPath"
                },
                renderBox:function (key, item) {},
                onAllBoxIsFilled:function () {},
                gotoContrastConfig:{
                    keyCsvParamName:"",
                    url:""
                },
                persistBoxValCookieKey:'uniqueBoxVals'
            },

            refreshHideShowStatusCookieVal:function () {
                var options = _CompareBox.options;
                var compareBdId = options.targetDiv + "CompareBdId";
                var cookieKey = options.targetDiv + "HideShowStatus";
                var status = 1;
                if ($("#" + compareBdId).css("display") == "none") {
                    status = 0;
                }
                $.cookie(cookieKey,status, {expires:7});
            },

            hideOrShow: function (status) {
                var options = _CompareBox.options;
                var compareBdId =options.targetDiv + "CompareBdId";
                var compareHideBtnId = options.targetDiv + "CompareHideBtn";

                if (status == 1) {
                    $("#" + compareBdId).show();
                    $("#" + compareHideBtnId).html("隐藏");

                } else {
                    $("#" + compareBdId).hide();
                    $("#" + compareHideBtnId).html("展开");
                }
            },

            isHide: function (compareBdId) {
                return $("#" + compareBdId).css("display") == "none";
            },

            showOrHideCompareBox: function (compareBdId) {
                var status = 0;
                if (_CompareBox.isHide(compareBdId)) {
                    status = 1;
                }
                _CompareBox.hideOrShow(status);
                _CompareBox.refreshHideShowStatusCookieVal();
            },

            init:function () {
                var options = _CompareBox.options;
                var uniqueBoxId = options.targetDiv+ 'UniqueBox';
                var compareHideBtnId = options.targetDiv + "CompareHideBtn";
                var fullTipDivId = options.targetDiv + "FullTipDiv";
                var gotoContrastBtnId =options.targetDiv + "FullTipDivGotoContrastBtn";
                var removeAllBtnId =options.targetDiv + "RemoveAllBtn";
                var compareBdId =options.targetDiv + "CompareBdId";
                var compareBoxHtml =
                    '<div id="pop-compare" class="pop-compare">' +
                    '    	<div class="pop-compare-tips fn-hide" id="'+fullTipDivId+'">'+options.tipsWhenFull+'</div>' +
                    '        <div class="pop-compare-inter">' +
                    '        	<div class="pop-compare-hd">' +
                    '            	<h3>我要对比</h3>' +
                    '                <span><a class="pop-compare-hd-showhide" href="javascript:" id="'+compareHideBtnId+'">隐藏</a></span>' +
                    '            </div>' +
                    '            <div class="pop-compare-bd" id="'+compareBdId+'">' +
                    '            	<div class="pop-compare-bd-box floatL" id="'+uniqueBoxId+'"></div>' +
                    '                <div class="pop-compare-bd-operate floatL">' +
                    '                	<a target="_blank" id="'+gotoContrastBtnId+'" class="pop-compare-bd-operate-goto-contrast pop-compare-active">对比</a>' +
                    '                    <a id="'+removeAllBtnId+'" class="pop-compare-del-items" href="javascript:">清空对比栏</a>' +
                    '                </div>' +
                    '            ' +
                    '            </div>' +
                    '        ' +
                    '        </div>' +
                    '    ' +
                    '    </div>';

                $("#" + options.targetDiv).html(compareBoxHtml);

                var $UniqueBox = $.UniqueBox({
                    targetDiv:uniqueBoxId,
                    containerClass:"pop-compare-bd-box",
                    getKeepPersistedValueHtmlTagSelector:options.keepPersistedValueHtmlTagConfig.getKeepPersistedValueHtmlTagSelector,
                    onAdd:function (key, box) {
                        //alert(options.keepPersistedValueHtmlTagConfig.getKeepPersistedValueHtmlTagSelector(key));
                        var keppPersistedValueHtmlTag = $(options.keepPersistedValueHtmlTagConfig.getKeepPersistedValueHtmlTagSelector(key));
                        keppPersistedValueHtmlTag.html(options.keepPersistedValueHtmlTagConfig.removeText);

                        var value = options.renderBox(key, keppPersistedValueHtmlTag);
                        $(box).html(value);

                        configContrastBtn(this.getFilledBoxCount());
                        _CompareBox.hideOrShow(1);
                        //_CompareBox.refreshHideShowStatusCookieVal();

                    },

                    configSubmitBtn : false,
                    setValueAbleWhenFull:false,
                    onRemove:function (key, box) {
                        $(options.keepPersistedValueHtmlTagConfig.getKeepPersistedValueHtmlTagSelector(key)).html(options.keepPersistedValueHtmlTagConfig.addText);
                        $("#" +fullTipDivId).hide();
                        $("#" + gotoContrastBtnId).addClass("pop-compare-active");

                        configContrastBtn(this.getFilledBoxCount());
                        _CompareBox.hideOrShow(1);
                        //_CompareBox.refreshHideShowStatusCookieVal();

                    },
                    onAllBoxIsFilled:function () {
                        var options = _CompareBox.options;
                        $("#" +fullTipDivId).show();
                        configContrastBtn(this.getFilledBoxCount());
                        _CompareBox.hideOrShow(1);
                    },
                    cookieKey : options.persistBoxValCookieKey

                });

                $("#" + removeAllBtnId).click(function () {
                    $UniqueBox.removeAll();
                });

                //显示隐藏对比栏配置
                /*var persistBoxValCookieKey = options.targetDiv + "HideShowStatus";
                var hideShowStatus = $.cookie(persistBoxValCookieKey);
                _CompareBox.hideOrShow(hideShowStatus);*/
                _CompareBox.hideOrShow(0);


                $("#" + compareHideBtnId).click(function () {
                    _CompareBox.showOrHideCompareBox(compareBdId);
                });

                //双击显示或隐藏
                $("#" + options.targetDiv).dblclick(function () {
                    _CompareBox.showOrHideCompareBox(compareBdId);
                });

                configContrastBtn($UniqueBox.getFilledBoxCount());

                function configContrastBtn(filledBoxCount) {
                    if(filledBoxCount < 2){
                        $("#" + gotoContrastBtnId).removeClass("pop-compare-active");
                        $("#" +gotoContrastBtnId).unbind("click");
                    } else {
                        $("#" +gotoContrastBtnId).addClass("pop-compare-active");
                        $("#" +gotoContrastBtnId).click(function () {
                            //var url = "/"
                            var param = $UniqueBox.getAllValCsvParam(options.gotoContrastConfig.keyCsvParamName);
                            window.location.href = options.gotoContrastConfig.url + "?" + param;
                        });
                    }
                }

                $UniqueBox.exposeInterface.call(_CompareBox);
            }
        };

        _CompareBox.options =  $.extend(_CompareBox.options, options);

        _CompareBox.init();


        return this;
    };


})(jQuery, window);
